// pages/business/agentapply/agentapply.js
var shihan = require('../../../common/fetch');
var config = require('../../../common/config');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    page: 1,
    page_size: 10,
    listflag: true,
    showLoading: true,
    list: [],
    has_more: true
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) { //index 1-代理商申请列表  2-转移审核列表
    this.setData({
      type_index: options.index
    })
    this.refreshlist()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 页面事件
   */
  refreshlist: function() { //agent_verify_list
    var that = this,
      title = "",
      url = "";
    if (that.data.type_index == 1) {
      title = "代理商申请";
      url = config.agent_verify_list;
    } else if (that.data.type_index == 2) {
      title = "转移审核";
      url = config.agent_shift_list;
    }
    wx.setNavigationBarTitle({
      title: title,
    })
    shihan.sendRequest(url, 'POST', {
        page: that.data.page,
        page_size: that.data.page_size
      })
      .then(function(response) {
        if (response.data.code == 0) {
          // console.log(response);
          if (response.data.data.sh_data.length > 0) {
            that.setData({
              page: that.data.page + 1,
              list: that.data.list.concat(response.data.data.sh_data),
              showLoading: false,
              listflag: true
            })
          } else {
            that.setData({
              showLoading: false,
              listflag: false
            })
          }
          if (that.data.list.length == response.data.data.sh_total_rows) {
            that.setData({
              has_more: false,
              showLoading: false,
              listflag: false
            })
          }
        } else {
          that.setData({
            showLoading: false,
            listflag: false
          })
        }
        wx.stopPullDownRefresh();
      }, function(error) {
        console.log(error);
      });
  },
  shiftApply: function(e) { //agent_shift_verify status-审核操作 1通过 2拒绝
    var that = this,
      status = 0,
      content = '',
      text = '',
      id = e.currentTarget.dataset.id,
      apply = e.currentTarget.dataset.apply;
    if (apply == 'agree') {
      status = 1;
      text = '同意';
      content = '是否同意该代理商转移';
    } else if (apply == 'refuse') {
      status = 2;
      text = '拒绝';
      content = '是否拒绝该代理商转移';
    }
    wx.showModal({
      content: content,
      cancelText: '再想想',
      confirmText: text,
      success: function(res) {
        if (res.confirm) {
          shihan.sendRequest(config.agent_shift_verify, 'POST', {
              id: id,
              status: status
            })
            .then(function(response) {
              if (response.data.code == 0) {
                // console.log(response);
                that.setData({
                  page: 1,
                  list: [],
                  showLoading: true
                });
                that.refreshlist()
              } else {
                shihan.showToast.call(this, response.data.msg);
              }
            }, function(error) {
              console.log(error);
            });
        }
      }
    })
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {
    this.setData({
      page: 1,
      list: [],
      showLoading: true
    });
    this.refreshlist()
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {
    var that = this
    if (that.data.listflag) {
      that.setData({
        listflag: false
      })
      this.refreshlist()
    }
  }
})